/* @(#)ProductType.java    V1.0
 *
 * Project: APEP 
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *       {CREATE_TIME} First release
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2007 Auto-Parts E-Procurement Co., Ltd. 
 *        No.251-1 Wuning Road, Shanghai, 200063, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Auto-Parts E-Procurement Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with APEP.
 *
 * Warning:
 * =============================================================================
 *   
 * 
 */
package system.base;

import system.data.ProductTypePageData;
import framework.core.*;
/*
	Author    :
	CreateTime:{CREATE_TIME}
*/
public class ProductTypeSrv extends IHandler
{

	public ProductTypeSrv() 
	{
		super();
	}
	private void deleteProductType(DBConnection con,SrvContext context) throws Exception
	{
		int[] ids = context.getInts("ids");
		ProductType obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = ProductType.fetch(con,String.valueOf(ids[i]));
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveProductType(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		int id = context.getInt("Id",Value.NULL_INT);
		ProductType obj;
		if(id==Value.NULL_INT)   //add
		{
			obj = ProductType.fetch();
    	}
    	else                     //update
    	{
    		obj = ProductType.fetch(con,String.valueOf(id));
    	}
		ProductTypePageData data = (ProductTypePageData)PageData.fromForm(context,ProductTypePageData.class);
		context.setValue("ProductTypePageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listProductType(DBConnection con,SrvContext context,String path) throws Exception
	{
		ProductTypeSearchEvent event = (ProductTypeSearchEvent)SearchEvent.fromForm(context,ProductTypeSearchEvent.class);
		context.setSessionValue("ProductTypeSearchEvent",event);
		context.setNextUrl(path+"ProductTypeList.jsp");
	}
	private void backProductType(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"ProductTypeList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "system/base";
		
		if(action_code.equalsIgnoreCase("listProductType"))
		{
			listProductType(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewProductType"))
		{
			int id = context.getInt("Id");
			ProductType obj = ProductType.fetch(con,String.valueOf(id));
			context.setValue("ProductType",obj);
			context.setNextUrl(path+"ProductTypeView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteProductType"))
		{
			context.setErrorUrl(path+"ProductTypeList.jsp");
			deleteProductType(con,context);
			backProductType(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddProductType"))
		{
			ProductType obj = ProductType.fetch();
			ProductTypePageData data = (ProductTypePageData)PageData.fromTable(obj,ProductTypePageData.class);
			context.setValue("ProductTypePageData",data);
			context.setNextUrl(path+"ProductTypeEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateProductType"))
		{
			int id = context.getInt("Id");
			ProductType obj = ProductType.fetch(con,String.valueOf(id));
			ProductTypePageData data = (ProductTypePageData)PageData.fromTable(obj,ProductTypePageData.class);
			context.setValue("ProductTypePageData",data);
			context.setNextUrl(path+"ProductTypeEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyProductType"))
		{
			int id = context.getInt("Id");
			ProductType obj = ProductType.copy(ProductType.fetch(con,String.valueOf(id)));
			ProductTypePageData data = (ProductTypePageData)PageData.fromTable(obj,ProductTypePageData.class);
			context.setValue("ProductTypePageData",data);
			context.setNextUrl(path+"ProductTypeEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveProductType"))
		{
			context.setErrorUrl(path+"ProductTypeEdit.jsp");
			saveProductType(con,context,loginContext);
			backProductType(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewProductType"))
		{
			context.setErrorUrl(path+"ProductTypeEdit.jsp");
			saveProductType(con,context,loginContext);
			ProductType obj = ProductType.fetch();
			ProductTypePageData data = (ProductTypePageData)PageData.fromTable(obj,ProductTypePageData.class);
			context.setValue("ProductTypePageData",data);
			context.setNextUrl(path+"ProductTypeEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListProductType"))
		{
			backProductType(con,context,path);
			return;
		}
		
		//add by  start

		//add by  end

		Logger.throwException("ProductTypeSrv","unknown action_code="+action_code);
	}
}
